<?php
namespace app\admin\controller;

use think\Db;
use think\Controller;
use think\Request;
use think\File;
use think\Model;
Db::startTrans();
class Profile extends Controller{
    public function profile($business_id){
        $get_id =  $business_id;
        $list = db("hotel")->where("id=".$get_id)->paginate(6);//这查的是数
        $page = $list->render();
        return view("profile",array("bo"=>$list,"page"=>$page,['business_id'=>$get_id]));
    }
    public function modify($business_id){
        $get = $business_id;
        $dates = Db::table("hotel")->where("id=".$get)->select();
        return view("modify",array("data"=>$dates));
    }
    public function modify_save($business_id){//修改
        $get = $business_id;
        $file = request()->file("file");
        $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
        $img  = $info->getSaveName();
        $request = Request::instance();
//        $array =array(
//            'longitude'=> $request->post('lng'),
//            'latitude'=> $request->post('lat'),
//            'name'=> $request->post('merchant_name'),
//            'img'=>$img,
//            'mobile'=>$request->post('mobile'),
//            'email'=>$request->post('email'),
//            'introduce'=>$request->post('merchant_introduction'),
//            'type'=>$request->post('merchant_if'),
//            'detailed'=>$request->post('sever_add')
//        );

        $text = Db::table("hotel")->where("id=".$get)->update(
            array('longitude'=> $_POST['lng'],
            'latitude'=>$_POST['lat'],
            'name'=> $_POST['merchant_name'],
            'img'=>$img,
            'phone'=>$_POST['phone'],
            'Mail'=>$_POST['Mail'],
            'introduce'=>$_POST['merchant_introduction'],
            'type'=>$_POST['merchant_if'],
            'detailed'=>$_POST['sever_add']));
        if($text){
            $data=Db::table("hotel")->where("id=".$get)->paginate(6);
            $page = $data->render();
            return view('Profile/profile', ["bo"=>$data,"business_id"=>$get,"page"=>$page]);
        }else{
            $this->error('修改失败','Profile/modify','',1);
        }
    }
    public function delete($business_id){//删除
        $get = $business_id;
        try{
            Db::table('room_release')->where("hotel_id=$get")->delete();
            Db::table('hotel')->where("id=$get")->delete();
            // 提交事务
            Db::commit();
            echo 'ok';
        } catch (\Exception $e) {
            // 回滚事务
            echo "chack";
            Db::rollback();
        }
        $this->redirect('profile', ['business_id'=>$get]);
    }
    public function batch_delete(){
        $getid = $_POST;
        if(!$getid) {
            $this->error("未选择记录");
        }else{
            $request =Db::table('hotel')->delete($getid);
            $say = '删除成功';
            if($request === false){
                $this->error('操作失败');
            }else{
                $this->success($say,'profile','',1);
            }
        }
    }
}
